package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.CommentMapper;
import com.mapper.UserMapper;
import com.po.Comment;
import com.po.User;
import com.result.Result;
import com.service.CommentService;
import com.vo.CommentVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @return
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>implements CommentService {
    private final  CommentMapper commentMapper;

    private final UserMapper userMapper;
    @Override
    public List<CommentVo> getComments(Long id) {
        List<CommentVo> commentVos = new ArrayList<>();

        // 获取所有顶级评论（根评论）
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getDiscoveryId, id).isNull(Comment::getRootCommentId);
        List<Comment> comments = commentMapper.selectList(wrapper);
        log.info("顶级评论:{}",comments);
        // 为每个顶级评论获取用户信息和回复
        for (Comment comment : comments) {
            // 获取顶级评论的用户信息
            User user = userMapper.selectById(comment.getUserId());

            // 创建顶级评论的 CommentVo
            CommentVo commentVo = new CommentVo();
            commentVo.setAvatar(user.getImage());
            commentVo.setNickname(user.getName());
            commentVo.setId(comment.getId());
            commentVo.setContent(comment.getContent());
            commentVo.setCreateTime(comment.getCreateTime());
            //获取当前顶级评论的子评论
            LambdaQueryWrapper<Comment> replyWrapper = new LambdaQueryWrapper<>();
            replyWrapper.eq(Comment::getRootCommentId, comment.getId());
            List<Comment> replyComments = commentMapper.selectList(replyWrapper);

            // 将子评论映射到 CommentVo
            List<CommentVo> replyCommentVos = new ArrayList<>();
            for (Comment replyComment : replyComments) {
                // 获取子评论的用户信息
                User replyUser = userMapper.selectById(replyComment.getUserId());

                // 创建子评论的 CommentVo
                CommentVo replyCommentVo = new CommentVo();
                replyCommentVo.setAvatar(replyUser.getImage());
                replyCommentVo.setNickname(replyUser.getName());
                replyCommentVo.setId(replyComment.getId());
                replyCommentVo.setContent(replyComment.getContent());
                replyCommentVo.setCreateTime(replyComment.getCreateTime());
                replyCommentVo.setParentCommentId(user.getId());
                replyCommentVo.setParentNickname(user.getName());
                replyCommentVo.setParentComment(comment.getContent());
                // 将子评论添加到子评论列表
                replyCommentVos.add(replyCommentVo);

            }

            // 将子评论设置到顶级评论中
            commentVo.setReplyComments(replyCommentVos);

            // 将顶级评论添加到列表中
            commentVos.add(commentVo);
        }

        // 第五步：返回包含子评论的顶级评论列表
        return commentVos;
    }


}






